package casts.facebook
{
   import casts.step.AbstractStep;
   import casts.step.IStep;
   
   import com.facebook.graph.Facebook;
   
   import command.DoTweenMax;
   
   import flash.external.ExternalInterface;
   
   public class fb_step_0 extends AbstractStep implements IStep
   {
      // fla
      public var btnLoginFB:MyButton;
      
      public function fb_step_0()
      {
         super();
         
         id = 'fb step 0';
         group = 'fb';
         containerId = 'fb main';
      }
      
      // --------------------- LINE ---------------------
      
      public function init():void
      {
         btnLoginFB.buttonMode = true;
         btnLoginFB.onClick = function()
         {
            if (!this.buttonMode) return;
            
            this.buttonMode = false;
            
            if (ExternalInterface.available)
            {
               fbLogin();
            }
            else
            {
               changeToStep('fb step 1');
            }
         };
      }
      
      public function destroy():void
      {
      }
      
      // ################### protected ##################
      
      override protected function atCastAdded():void
      {
         init();
         
         alpha = 0;
         x = 0;
         y = -10;
         addCommand(
            new DoTweenMax(this, 0.3, { alpha:1, y:0 } )
         );
      }
      
      override protected function atCastRemoved():void
      {
         destroy();
      }
      
      // #################### private ###################
      
      // before login facebook, remember to do init() job at first
      private function fbLogin():void
      {
         trace("fb_step_0.fbLogin()");
         addLoading();
         
         Facebook.login(fbLogin_Complete, { perms:DEF.PERMISSION } );
      }
      
      // login complete
      private function fbLogin_Complete(success:Object, fail:Object):void
      {
         trace("fb_step_0.fbLogin_Complete() | Facebook.getSession() =", Facebook.getSession());
         if (Facebook.getSession().uid)
         {
            fbGetProfile();
         }
         else
         {
            removeLoading();
            
            MyAlert.jsShow('登入facebook失敗…請重試');
         }
      }
      
      // get user personal info
      private function fbGetProfile():void
      {
         Facebook.api('/me', fbGetProfile_Complete);
      }
      
      // save user personal info
      private function fbGetProfile_Complete(success:Object, fail:Object):void
      {
         trace("fb_step_0.fbGetProfile_Complete()");
         if (success)
         {
            // save
            DEF.profile = success;
            
            // debug
            trace();
            trace('>>> get my facebook profile.');
            trace('----------------------------');
            for (var v:String in success)
            {
               trace(v, '=', success[v]);
            }
            trace('----------------------------');
            
            // 抓大頭照
            Facebook.api('me?fields=picture&type=square&', fbGetMyPicture_Complete);
         }
         else
         {
            MyAlert.jsShow('取得個人facebook資訊失敗…請重試');
         }
      }
      
      private function fbGetMyPicture_Complete(success:Object, fail:Object):void
      {
         trace("fb_step_0.fbGetMyPicture_Complete()");
         
         if (success)
         {
            var url:String = String(success.picture);
            DEF.profile.picture_s = url;
         }
         else
         {
            MyAlert.jsShow('取得個人大頭照失敗…請重試');
         }
      }
      
      // --------------------- LINE ---------------------
      
   }
   
}